//
//  CYUserCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/29.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYUserCell.h"
@interface CYUserCell()
@property (nonatomic, strong) CYImageView *imgView;
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) UISwitch *on_off;
@end
@implementation CYUserCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.line.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self setSeparatorInset:UIEdgeInsetsZero]; /*cell的分割线，左对齐*/
        [self setLayoutMargins:UIEdgeInsetsZero];  /*cell的分割线，左对齐*/
    }
    
    return self;
}

- (CYImageView *)imgView{
    if (!_imgView) {
        _imgView = [[CYImageView alloc]initWithImage:[UIImage imageNamed:_img]];
        [self addSubview:_imgView];
        
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(37)));
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.height / 2.0, self.bounds.size.height / 2.0));
        }];
    }
    return _imgView;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_title];
        [self addSubview:_titleLBL];
    }
    CGFloat width = (_imgView)?(AL(PT(37 + 20) + self.bounds.size.height / 2.0)):AL(PT(20));
    [_titleLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(width);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(_titleLBL.bounds.size);
    }];
    
    return _titleLBL;
}


- (UISwitch *)on_off{
    if (!_on_off) {
        _on_off = [[UISwitch alloc]init];
        [_on_off addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_on_off];
        
        [_on_off mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(- AL(PT(24)));
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(AL(44), AL(34)));
        }];
    }
    
    return _on_off;
}

- (void)setTitle:(NSString *)title{
    if (_title != title) {
        _title = title;
        self.titleLBL.hidden = (title.length > 0)?NO:YES;
    }
}

- (void)setImg:(NSString *)img{
    if (_img != img) {
        _img = img;
        self.imgView.hidden = (img.length > 0)?NO:YES;
    }
}

- (void)setOnStr:(NSString *)onStr{
    if (_onStr != onStr) {
        _onStr  = onStr;
        self.on_off.on = [onStr isEqualToString:@"YES"]?YES:NO;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
}

- (void)switchAction:(UISwitch *)sender{
    [self.delegate actionForCYUserCell:sender.on cellTitle:_title];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
